package professor.aula07.com.fafica.cliente;

import java.net.URL;
import java.sql.*;
import java.util.*;
import java.io.*;

import aula07.com.fafica.util.ConnectionManager;
import aula07.com.fafica.util.RepositorioException;

public class RepositorioClienteBDR implements IRepositorioCliente {

    private Statement statement = null;

    public RepositorioClienteBDR() {
        //this.contas = RepositorioContasBDR.getInstance();
        this.statement = ConnectionManager.reservaStatement("mysql");
    }

    public void inserir(Cliente cliente) throws RepositorioException {
        try {
            //Statement statement = ConnectionManager.reservaStatement();
            if (cliente != null) {
                try {
                	this.statement.executeUpdate("INSERT INTO CLIENTE VALUES ('" +
				                			cliente.getCpf()  + "', '" +
				                			cliente.getNome() + "')");
                }
                catch (SQLException e) {
                    throw new RepositorioException(e);
    		    }
            }
        } finally {
		   // ConnectionManager.liberaStatement();
        }
    }

	public void remover(String cpf) throws ClienteNaoEncontradoException, RepositorioException {
        try{
            //Statement statement = ConnectionManager.reservaStatement();
		    int resultado = this.statement.executeUpdate("DELETE FROM CLIENTE WHERE cpf='" + cpf + "'");
            if (resultado == 0) {
                throw new ClienteNaoEncontradoException(cpf);

            } else {
            }
		} catch(SQLException e){
            throw new RepositorioException(e);
		} finally {
		    //ConnectionManager.liberaStatement();
		}
	}

	public Cliente procurar(String cpf) throws ClienteNaoEncontradoException, RepositorioException {
   	    Cliente cliente = null;
        try {
            //Statement statement = ConnectionManager.reservaStatement();
            ResultSet resultset = this.statement.executeQuery("SELECT * FROM CLIENTE WHERE cpf = '" + cpf + "'");
            if (resultset.next()) {
                cliente = new Cliente(resultset.getString("cpf"), resultset.getString("nome"));
                //resultset.close();
                //this.recuperaRelacionamento(cliente);
            } else {
                throw new ClienteNaoEncontradoException(cpf);
            }
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
		   // ConnectionManager.liberaStatement();
		}
		return cliente;
	}

	public void atualizar(Cliente cliente) throws ClienteNaoEncontradoException, RepositorioException {
	    try {
    	    //Statement statement = ConnectionManager.reservaStatement();
            if (cliente != null) {
                try {
                    int resultado = this.statement.executeUpdate("UPDATE CLIENTE SET nome = '" + cliente.getNome() +
             		                                        "' WHERE cpf = '" + cliente.getCpf() + "'");
                     if (resultado == 0) {
                        throw new ClienteNaoEncontradoException(cliente.getCpf());
                    } else {
                        //this.atualizaRelacionamento(cliente);
                    }
                }
                catch (SQLException e) {
    			    throw new RepositorioException(e);
    		    }
            }
        } finally {
		    //ConnectionManager.liberaStatement();
		}
	}

	public boolean existe(String cpf) throws RepositorioException {
   	    boolean resposta = false;
        try {
            //Statement statement = ConnectionManager.reservaStatement();
            ResultSet resultset = this.statement.executeQuery("SELECT * FROM CLIENTE WHERE cpf = '" + cpf + "'");
            resposta = resultset.next();
			//resultset.close();
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
		    //ConnectionManager.liberaStatement();
		}
		return resposta;
    }
	
	public ArrayList<Cliente> listar() throws RepositorioException {
		ArrayList<Cliente> clientes = new ArrayList<Cliente>();
        try {
            //Statement statement = ConnectionManager.reservaStatement();
            ResultSet resultset = this.statement.executeQuery("SELECT * FROM CLIENTE");
            while(resultset.next()) {
            	Cliente cliente = new Cliente(resultset.getString("CPF"), resultset.getString("nome"));
            	clientes.add(cliente);
            }
			//resultset.close();
        } catch (SQLException e) {
			throw new RepositorioException(e);
		} finally {
		    //ConnectionManager.liberaStatement();
		}
        
        return clientes;
	}

}